/**
 * 
 */
package com.magnusart.scrumify.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.magnusart.scrumppm.model.Story;
import com.magnusart.scrumppm.model.Task;
import com.magnusart.scrumppm.model.TaskBuilder;

/**
 * @author Magnus Andersson
 *
 */
public class HsqlDbTaskDAO extends HsqlDbAbstactDAO<Story, Task, TaskBuilder>
{

	public HsqlDbTaskDAO( HsqlDbDAOFactory factory ) {
		super( factory );
	}
	
	{
		sqlAdd = "INSERT INTO Tasks( " +
		"storyID, " +
		"taskDescription, " +
		"isDone, " +
		"taskOrder ) " + 
		"VALUES( ?, ?, ?, ? )";
		
		sqlGetAll = "SELECT taskID, " +
		"taskDescription, " +
		"isDone, " +
		"taskOrder " +
		"FROM Tasks WHERE storyID = ?";
		
		sqlGetOne = "SELECT taskID, " +
		"taskDescription, " +
		"isDone, " +
		"taskOrder " +
		"FROM Tasks WHERE taskID = ?";
		
		sqlUpdate = "UPDATE Tasks SET " +
		"storyID = ?, " +
		"taskDescription = ?, " +
		"isDone = ?,  " +
		"taskOrder = ?  " +
		"WHERE taskID = ? ";
		
		sqlDelete = "DELETE FROM Tasks WHERE taskID = ?";
	}



	@Override
	Task pepareUpdate( Story parent, Task entry, TaskBuilder builder,
			PreparedStatement pstmt) throws SQLException {
		Task updated = new Task( builder );
		
		pstmt.setInt( 1, parent.getID() );
		pstmt.setString( 2, updated.getDescription() );
		pstmt.setBoolean( 3, updated.isDone() );
		pstmt.setInt( 4, updated.getOrder() );
		pstmt.setInt( 5, entry.getID() );
		
		return updated;
	}

	@Override
	void perpareAdd( Story parent, Task entry, PreparedStatement pstmt)
			throws SQLException {
		pstmt.setInt( 1, parent.getID() );
		pstmt.setString( 2, entry.getDescription() );
		pstmt.setBoolean( 3, entry.isDone() );
		pstmt.setInt( 4, entry.getOrder() );
	}

	@Override
	void prepareDelete( Task entry, PreparedStatement pstmt)
			throws SQLException {
		pstmt.setInt( 1, entry.getID() );
		
	}

	@Override
	void prepareGetAll( Story parent, PreparedStatement pstmt)
			throws SQLException {
		pstmt.setInt( 1, parent.getID() );
	}

	@Override
	void prepareGetOne( Task entry, PreparedStatement pstmt)
			throws SQLException {
		pstmt.setInt( 1, entry.getID() );
	}

	@Override
	Task fetchBuilderData( ResultSet rs, TaskBuilder builder)
			throws SQLException {
		builder = new TaskBuilder();
		builder.setID( rs.getInt( "taskID" ) );
		builder.setDescription( rs.getString( "taskDescription" ) );
		builder.setDone( rs.getBoolean( "isDone" ) );
		builder.setOrder( rs.getInt( "taskOrder" ) );
		return new Task( builder );
	}



	@Override
	Task addedEntry( DAOFactory factory, Task entry, TaskBuilder builder, int addedID) {
		builder = new TaskBuilder( entry );
		builder.setID( addedID );
		
		return new Task( builder );
	}

	
}
